package com.gtgj.widget.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by luoj@huoli.com on 2019-11-28.
 */
public class LibDateUtils {

    public static SimpleDateFormat yyyy_MM_dd_HH_mm_ss() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    }

    public static SimpleDateFormat yyyyMM() {
        return new SimpleDateFormat("yyyyMM", Locale.getDefault());
    }

    public static SimpleDateFormat yyyy_MM() {
        return new SimpleDateFormat("yyyy-MM", Locale.getDefault());
    }

    public static SimpleDateFormat yyyyMMdd() {
        return new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
    }

    public static SimpleDateFormat yyyCMMCddC() {
        return new SimpleDateFormat("yyyy年M月d日", Locale.getDefault());
    }

    public static SimpleDateFormat yyyCMMCddCHH_MM() {
        return new SimpleDateFormat("yyyy年M月d日 HH:mm", Locale.getDefault());
    }

    public static SimpleDateFormat yyyy_MM_dd() {
        return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    }

    public static SimpleDateFormat yyyy_MM_dd_HH_mm() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
    }

    public static SimpleDateFormat yyyy_MM_dd_HH_mm_ss_SSS() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault());
    }

    public static SimpleDateFormat yyyy_MM_dd_HH_mm_ss_S() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.getDefault());
    }

    public static SimpleDateFormat HH_mm() {
        return new SimpleDateFormat("HH:mm", Locale.getDefault());
    }

    public static SimpleDateFormat HH_mm_C() {
        return new SimpleDateFormat("H时m分", Locale.getDefault());
    }

    public static SimpleDateFormat Md() {
        return new SimpleDateFormat("M月d日", Locale.getDefault());
    }

    public static SimpleDateFormat MdHm() {
        return new SimpleDateFormat("M月d日 HH:mm", Locale.getDefault());
    }

    public static SimpleDateFormat MMdd() {
        return new SimpleDateFormat("MMdd", Locale.getDefault());
    }

    public static SimpleDateFormat MMddC() {
        return new SimpleDateFormat("MM月dd日", Locale.getDefault());
    }

    public static SimpleDateFormat MM_dd() {
        return new SimpleDateFormat("MM-dd", Locale.getDefault());
    }

    public static SimpleDateFormat yyyyMMddHHmm() {
        return new SimpleDateFormat("yyyyMMdd HH:mm", Locale.getDefault());
    }

    public static SimpleDateFormat M() {
        return new SimpleDateFormat("M月", Locale.getDefault());
    }


    /*
     * 将0000-00-00 00:00:00格式转化成Date对象
     */
    public static Date convertYMDHMSStringToDate(String ymdhmsString) {
        try {
            return yyyy_MM_dd_HH_mm_ss().parse(ymdhmsString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * 将yyy-MM-dd/yyyyMMdd格式的日期字符串转成"9月11日"
     */
    public static String getMDString(String date) {
        try {
            String sDate = convertDateToStandardFormat(date);
            Date dateO = yyyyMMdd().parse(sDate);
            String md = Md().format(dateO);
            return md;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /*
     * 将yyy-MM-dd/yyyyMMdd格式的日期字符串转成"09月11日"
     */
    public static String getMMDDString(String date) {
        try {
            String sDate = convertDateToStandardFormat(date);
            Date dateO = yyyyMMdd().parse(sDate);
            String md = MMddC().format(dateO);
            return md;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String convertDateToStandardFormat(String date) {
        date = date.trim();
        if (date.length() == 8) {
            return date;
        } else if (date.length() == 10) {
            return date.replace("-", "");
        } else {
            return null;
        }
    }

    public static long diffDays(String first, String second) {
        long result = 0;
        if (TextUtils.isEmpty(first) || TextUtils.isEmpty(second)) {
            return result;
        }

        try {
            String firstformate = convertDateToStandardFormat(first);
            String secondformate = convertDateToStandardFormat(second);

            Date f = yyyyMMdd().parse(firstformate);
            Date d = yyyyMMdd().parse(secondformate);
            result = (f.getTime() - d.getTime()) / (1000 * 60 * 60 * 24);

        } catch (Exception e) {
            e.printStackTrace();
            result = 0;
        }
        return result;

    }
}
